/**
 *  mailbox.h - Related routines and structs of mailbox.
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __MAILBOX_H__
#define __MAILBOX_H__

#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/mem.h"
#include "include/irq.h"
#include "include/core.h"
#endif


// struct message - It describes the style of message
typedef struct message {
    uint32 key_id;    // Message ID that identify a message.
    int32 send_id;    // ID of sending task.
    int32 rev_id;     // ID of receiving task.
    void *pmsg;        // Pointer to the address of message.
} msg_t;

// mailbox - It is prototype of mailbox
typedef struct mailbox {
    uint32 size;       // The size of mailbox.
    uint32 used;       // The number of used space of mailbox.
    msg_t *start;       // Pointer to the start address of mailbox.
    msg_t *end;         // Pointer to the end address of mailbox.
    msg_t *pos;         // current write-read postion of mailbox.
} mail_t;

// Created a new mailbox
extern mail_t *mboxInit(uint32 Size);

// Sending a message to a mailbox
extern bool_t mboxSend(mail_t *pMail, // Pointer to the mailbox.
                       uint32 Key,  // Serial number of message.
                       int32 RevID, // ID of Receiving task.
                       void *pMsg);

// Retrieve message from mailbox
extern void *mboxRev(mail_t *pMail,   // Pointer to the mailbox.
                     uint32 Key,     // Serial number of message.
                     int32 SendID);  // ID of sending task.

// Get current used space of mailbox.
extern int32 mboxGetUsed(const mail_t *pMail);

// Clear a mailbox
extern void mboxClear(mail_t *pMail);

// Delete a mailbox
extern void mboxDel(mail_t *pMail);


#endif

